package com.isesol.ime.service;

import com.alibaba.fastjson.*;
import com.alibaba.fastjson.serializer.*;
import com.google.common.collect.*;
import com.isesol.arch.common.mq.*;
import com.isesol.ime.dal.model.dto.*;
import com.isesol.ime.service.mq.topic.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;

import javax.annotation.*;
import java.util.*;

@Component
public class PushService{

	@Resource
	SubscriptionService subscriptionService;

	@Autowired(required = false)
	MsgSender msgSender;

	public void pushAll(String obj){

		Map    map         = JSON.parseObject(obj);
		String machineType = map.get("machineType").toString();

		List<PushDTO> pushDTOList = subscriptionService.findAllMachineByMachineType(machineType);

		for (PushDTO pushDTO : pushDTOList){

			Map<String, String> infoMap = Maps.newHashMap();
			infoMap.put("machineId", pushDTO.getMachineId());

			String jsonMsg = JSON.toJSONString(infoMap, SerializerFeature.WriteClassName);

			msgSender.sendMessage(jsonMsg, PushAllProcessTopic.class);
		}

	}

	public void pushAllProcess(String obj) throws Exception{

		Map    map       = JSON.parseObject(String.valueOf(obj));
		String machineId = map.get("machineId").toString();

		List<PushDTO> pushDTOList = subscriptionService.findOneSubByMachineId(machineId);

		for (PushDTO pushDTO : pushDTOList){

			subscriptionService.sendDownloadOrder(pushDTO.getMachineId(), String.valueOf(pushDTO.getSubId()));

		}
	}

}
